/**
 * String新增原型链3个属性
 */
interface String {
  pointLength(): number;
  pointAt(index: number): string;
  sliceByPoint(start: number, end: number): string;
}

/** 字符串码元长度，👿字符占有2个码元 */
String.prototype.pointLength = function () {
  let len = 0;
  for (let i = 0; i < this.length; ) {
    const codePoint = this.codePointAt(i);
    i += codePoint! > 0xffff ? 2 : 1;
    len++;
  }
  return len;
};

/**码点位置字符  */
String.prototype.pointAt = function (index: number) {
  let curIndex = 0;
  for (let i = 0; i < this.length; ) {
    const codePoint = this.codePointAt(i);
    if (curIndex === index) {
      return String.fromCodePoint(codePoint!);
    }
    i += codePoint! > 0xffff ? 2 : 1;
    curIndex++;
  }
  return "";
};

/** 截取字符串，根据码点位置 */
String.prototype.sliceByPoint = function (start: number, end: number) {
  let result = "";
  for (let i = start; i < end; i++) {
    result += this.pointAt(i);
  }
  return result;
};
